﻿using System;
using System.Windows.Forms;
using OutlookContactsSync.Domain.Properties;
using OutlookContactsSync.Domain.Views;
using Settings = OutlookContactsSync.Domain.Globals.Settings;

namespace OutlookContactsSync
{
    public partial class SettingsForm : BaseForm, IOutlookSyncSettingsView   
    {
        private Settings.Status _screenStatus;

        public SettingsForm()
        {
            InitializeComponent();
            InitializeControls();
        }

        #region IOutlookSyncSettingsView Members

        public override void ShowForm()
        {
            ShowDialog();
        }

        public event EventHandler Save;
        public event EventHandler Reset;

        #endregion

        #region IOutlookSyncSettingsView properties

        public Settings.Status ScreenStatus
        {
            get
            {
                return _screenStatus;
            }
            set
            {
                _screenStatus = value;
                InitializeButtons();
            }
        }

        public bool RemoveOldUsersChecked
        {
            get { return checkBoxRemoveOldUsers.Checked; }
            set { checkBoxRemoveOldUsers.Checked = value; }
        }

        public bool BusinessChecked
        {
            get { return checkBoxBusinessPhone.Checked; }
            set { checkBoxBusinessPhone.Checked = value; }
        }

        public bool BusinessFaxChecked
        {
            get { return checkBoxBusinessFax.Checked; }
            set { checkBoxBusinessFax.Checked = value; }
        }

        public bool CompanyChecked
        {
            get { return checkBoxCompany.Checked; }
            set { checkBoxCompany.Checked = value; }
        }

        public bool DisplayAsChecked
        {
            get { return checkBoxDisplayAs.Checked; }
            set { checkBoxDisplayAs.Checked = value; }
        }

        public bool EmailChecked
        {
            get { return checkBoxEmail.Checked; }
            set { checkBoxEmail.Checked = value; }
        }

        public bool FileAsChecked
        {
            get { return checkBoxFileAs.Checked; }
            set { checkBoxFileAs.Checked = value; }
        }

        public bool FullNameChecked
        {
            get { return checkBoxFullName.Checked; }
            set { checkBoxFullName.Checked = value; }
        }

        public bool HomeChecked
        {
            get { return checkBoxHomePhone.Checked; }
            set { checkBoxHomePhone.Checked = value; }
        }

        public bool ImAddressChecked
        {
            get { return checkBoxImAddress.Checked; }
            set { checkBoxImAddress.Checked = value; }
        }

        public bool JobTitle
        {
            get { return checkBoxJobTitle.Checked; }
            set { checkBoxJobTitle.Checked = value; }
        }

        public bool MobilePhone
        {
            get { return checkBoxMobilePhone.Checked; }
            set { checkBoxMobilePhone.Checked = value; }
        }

        public bool Picture
        {
            get { return checkBoxPicture.Checked; }
            set { checkBoxPicture.Checked = value; }
        }

        public bool WebPageAddress
        {
            get { return checkBoxWebPageAddress.Checked; }
            set { checkBoxWebPageAddress.Checked = value; }
        }

        public string ContactsCategory
        {
            get { return textBoxCategory.Text; }
            set { textBoxCategory.Text = value; }
        }

        #endregion

        private void InitializeButtons()
        {
            ApplyButton.Enabled = (ScreenStatus == Settings.Status.SettingsChanged);
        }

        private void InitializeControls()
        {
            SelectPropertieslabel.Text = Resources.SelectSroperties;
            removeOldUserslabel.Text = Resources.RemoveOldUsers;
            Text = Resources.SettingsFormTitle;

            //Checkboxes
            checkBoxFullName.Text = Resources.FullName;
            checkBoxCompany.Text = Resources.Company;
            checkBoxJobTitle.Text = Resources.JobTitle;
            checkBoxFileAs.Text = Resources.FileAs;
            checkBoxEmail.Text = Resources.Email;
            checkBoxDisplayAs.Text = Resources.DisplayAs;
            checkBoxWebPageAddress.Text = Resources.WebPage;
            checkBoxImAddress.Text = Resources.IMAdress;
            checkBoxBusinessPhone.Text = Resources.BusinessPhone;
            checkBoxBusinessFax.Text = Resources.BusinessFax;
            checkBoxHomePhone.Text = Resources.HomePhone;
            checkBoxMobilePhone.Text = Resources.Mobile;
            checkBoxPicture.Text = Resources.ContactPicture;
            contactsCategory.Text = Resources.ContactsCategory;

            ScreenStatus = Settings.Status.SettingsUnchanged;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            SaveSettings();
            Close();
        }

        private void ApplyButton_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void SaveSettings()
        {
            if (ScreenStatus == Settings.Status.SettingsChanged)
            {
                if (Save != null)
                {
                    Save(this, EventArgs.Empty);
                }

                ScreenStatus = Settings.Status.SettingsSaved;
            }
        }

        private void ControlPropertyChanged(object sender, EventArgs e)
        {
            ScreenStatus = Settings.Status.SettingsChanged;
        }

        private void Resetbutton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Resources.AreYouSureYouWantToReset, 
                                "Reset", 
                                MessageBoxButtons.YesNo,
                                MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (Reset != null)
                {
                    Reset(this, EventArgs.Empty);
                }

                ScreenStatus = Settings.Status.SettingsChanged;
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}